home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 919 < prev    next >
Encoding:
Text File  |  1996-08-05  |  5.7 KB  |  211 lines

  1. Path: news.pix.za!usenet
  2. From: js000021@pixie.co.za (Smit JS)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: CPU time measurement
  5. Date: Wed, 10 Jan 1996 07:06:44 GMT
  6. Organization: PiX - Proxima information X-change
  7. Message-ID: <4d06ks$p7j@hawk.pix.za>
  8. References: <4cs60j$ilb@azure.acsu.buffalo.edu>
  9. NNTP-Posting-Host: 196.23.60.105
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. naras-r@acsu.buffalo.edu (Ram Prasad) wrote:
  13.  
  14. >Hi,
  15.  
  16. >Is there a standard acceptable way of measuring the 
  17. >number of (milli)seconds that different pieces of code take 
  18. >to execute in a system?
  19.  
  20. >I want to know the times taken by different subroutines
  21. >in my code. (I am looking for something that will give
  22. >the time to execute without interference from other
  23. >processes and programs that may be running in the system)
  24.  
  25. >I found "getrusage" in the man pages, but I am not sure if
  26. >that's what I should use.
  27.  
  28. >thanks,
  29.  
  30.  
  31. >-- 
  32. >Ram 
  33. >-- 
  34. >Ram Prasad                             naras-r@acsu.buffalo.edu      
  35.  
  36. Hi Ram,
  37. The following works well. I cannot remember where I got it from.
  38. Johan Smit
  39. js000021@pixie.co.asm  
  40. public    _start
  41. public    _stop
  42. ;use with _stop.asm from C
  43. ;/*--------------------------------------------------------*/    
  44. ;/*----------------Use in C file---------------------------*/    
  45. ;
  46. ;extern    void start();
  47. ;extern    void stop();
  48. ;
  49. ;unsigned int timer_sec,timer_milli,timer_micro,tick_count,adjustm=29;
  50. ;/*adjustm to be adjusted for a machine. Set adjustm to 0 and call
  51. start()
  52. ;and stop() with no intervening code. Set the resultant value into
  53. adjustm*/
  54. ;  start();
  55. ;routines to time here
  56. ;  stop();
  57. ;  printf(" %d.%3.3d%3.3d secs\n",timer_sec,timer_milli,timer_micro);
  58. ;/*----------------Use in C file---------------------------*/    
  59. ;/*--------------------------------------------------------*/    
  60.  
  61. extrn        _timer_sec:word
  62. extrn        _timer_milli:word
  63. extrn        _timer_micro:word
  64. extrn        _tick_count:word
  65. extrn        _adjustm:word
  66.  
  67. timer_low    equ    ds:[006ch]
  68. bios_dataseg    equ    0040h
  69. timer_mode    equ    43h
  70. timer0        equ    40h
  71.  
  72. _TEXT    SEGMENT    BYTE PUBLIC 'CODE'
  73.     ASSUME CS:_TEXT,DS:DGROUP
  74. ;/*--------------------------------------------------------*/    
  75. ;/*---------------------_start-----------------------------*/
  76. _start proc near
  77. ;Called from C small model to determine time taken by a function
  78. ;call _start at start of function
  79. ;call _stop at end of function
  80.  
  81.     push    ax
  82.     push    dx
  83.     push    ds
  84.     mov    _timer_micro,0        ;reset variables
  85.     mov    _timer_milli,0
  86.     mov    _timer_sec,0
  87. ;/*------------Initialise counter of 8253------------------*/
  88. ;note timer mode changed from mode 3 to mode 2. The timer period and
  89. interrupt
  90. ;functions are unaffected, the 8259 is initialised to be edge
  91. sensitive.
  92. ;The counter now decrement by 1
  93.     mov    al,00110100b    ;ctr0,lsb then msb,mode2 binary
  94.     out    timer_mode,al    ;mode register of 8253
  95.     sub    ax,ax        ;0 results in max count
  96.     out    timer0,al    ;lsb
  97.     out    timer0,al    ;msb
  98. ;/*------------Read bios time-of-day-----------------------*/
  99.     mov    dx,bios_dataseg
  100.     mov    ds,dx
  101.     mov    ax,timer_low    ;get count
  102.     pop    ds        ;back to this dataseg
  103.     mov    _tick_count,ax
  104.     pop    dx
  105.     pop    ax
  106.     ret    
  107. _start    endp
  108. ;/*--------------------------_start-----------------------*/
  109. ;/*-------------------------------------------------------*/    
  110. ;/*--------------------------_stop------------------------*/
  111. _stop PROC near
  112.     push    ax
  113.     push    bx
  114.     push    dx
  115.     push    ds        ;save dataseg
  116. ;elapsed time since _timer_start consists of:
  117. ;    (1) timer count intervals of 840ns
  118. ;    (2) interrupt ticks of 55ms
  119. ;/*-----------------read counter 0 of 8253-----------------*/
  120.     mov    al,0        ;latch counter for read
  121.     cli            ;int off until bios tod is read
  122.     out    timer_mode,al    ;8253 mode register
  123.     in    al,timer0
  124.     mov    dl,al
  125.     in    al,timer0
  126.     mov    dh,al        ;dx now have the count
  127. ;/*----------calculate the timer_micro component-----------*/
  128.     mov    ax,65535    ;maxcount
  129.     sub    ax,dx        ;timer count
  130.     mul    timer_convert    ;838.096 ns per count
  131.     div    tenthousand    ;time in microsecs
  132.     mov    _timer_micro,ax    ;save microsecs,rounded to nsec
  133.     cmp    dx,5000
  134.     jb    cont        ;round down
  135.     inc    _timer_micro    ;round up
  136. ;*/-----------------get bios time of day-------------------*/
  137. cont:    mov    dx,bios_dataseg
  138.     mov    ds,dx
  139.     mov    ax,timer_low    ;get the reading
  140.     pop    ds        ;back to this dataseg
  141.     sti            ;interrupts on
  142.     sub    ax,_tick_count    ;_timer_start reading
  143.     mul    count_convert    ;54.925 msec per tick
  144.     div    thousand
  145.     mov    count_milli,ax    ;save millisec part
  146.     mov    count_micro,dx    ;save microsec part
  147. ;/*----------------------check for jitter------------------*/
  148.     cmp    ax,0        ;check if elapsed time is 'small'
  149.     jne    jitter_ok    ;no problem
  150.     mov    ax,_adjustm
  151.     cmp    _timer_micro,ax
  152.     jae    jitter_ok    ;if no jitter,ok
  153.     mov    _timer_micro,ax    ;else -ve time, so fix
  154. ;/*-Combine timer and count values,result in time variables-*/
  155. jitter_ok:
  156.     mov    ax,dx        ;get count_micro
  157.     add    ax,_timer_micro    ;sum micro fields
  158.     cmp    ax,_adjustm    ;check for underflow
  159.     jae    compensate    ; no problem
  160.     dec    count_milli    ;borrow
  161.     add    ax,1000
  162. compensate:
  163.     sub    ax,_adjustm    ;compensate for time delays
  164.     mov    _timer_micro,ax
  165.     cmp    ax,1000        ;check for overflow
  166.     jb    field_ok
  167.     sub    dx,dx        ;timer_micro field too large
  168.     div    thousand    ;so carry out into timer_milli
  169.     mov    _timer_milli,ax
  170.     mov    _timer_micro,dx
  171. field_ok:
  172.     mov    ax,count_milli    ;sum milli fields
  173.     add    _timer_milli,ax
  174.     cmp    _timer_milli,1000;check for overflow
  175.     jb    done
  176.     sub    dx,dx
  177.     mov    ax,_timer_milli
  178.     div    thousand
  179.     mov    _timer_sec,ax
  180.     mov    _timer_milli,dx
  181. done:    pop    dx
  182.     pop    bx
  183.     pop    ax
  184.     ret
  185. _STOP    endp    
  186. ;/*------------------------_stop---------------------------*/
  187. ;/*--------------------------------------------------------*/    
  188.  
  189. _TEXT    ENDS
  190.  
  191. DGROUP    GROUP     _DATA,_BSS
  192. _DATA    SEGMENT    WORD PUBLIC 'DATA'
  193. ;initialised data segment
  194. timer_convert    dw    8381        ;838.096 nsec per count
  195. count_convert    dw    54925        ;54.925 msec per tick
  196. tenthousand    dw    10000
  197. thousand    dw    1000
  198.  
  199. _DATA    ENDS
  200. _BSS    SEGMENT    WORD PUBLIC 'BSS'
  201. ;uninitialised data segment
  202.  
  203. count_micro    dw    ?        ;calc from interrupt ticks
  204. count_milli    dw    ?        ;calc from interrupt ticks
  205.  
  206.  
  207. _BSS    ENDS
  208.     END
  209. 
  210.  
  211.